import 'package:flutter/widgets.dart';
import 'dart:math';

class FoldWidget extends StatefulWidget {

  final Widget titleWidget;
  final Widget contentWidget;
  final bool isFold;
  const FoldWidget({Key key,this.titleWidget,this.contentWidget,this.isFold}) : super(key: key);

  @override
  _FoldWidgetState createState() => _FoldWidgetState();
}

class _FoldWidgetState extends State<FoldWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _heightAnimation;
  Animation _rotationXAnimation;

  @override
  void initState() {
    super.initState();
    _controller =  AnimationController(duration: Duration(milliseconds: 500), vsync: this);
    _heightAnimation = Tween(begin: 0.0, end: 1.0).chain(CurveTween(curve: Curves.ease)).animate(_controller);
    _rotationXAnimation = Tween(begin: -pi/2, end: 0.0).chain(CurveTween(curve: Curves.easeIn)).animate(_controller);
    _rotationXAnimation.addListener(() {
      setState(() {});
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  void didUpdateWidget(FoldWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if(widget.isFold){
      _controller.forward();
    }else{
      _controller.reverse();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        widget.titleWidget,
        Transform(
          transform: Matrix4.rotationX(_rotationXAnimation.value),
          alignment: Alignment.topCenter,
          child: SizeTransition(
              sizeFactor:_heightAnimation,
              axis: Axis.vertical,
              child: widget.contentWidget
          )
        )
      ]
    );
  }

/**
 * SizeTransition(
    sizeFactor:_heightAnimation,
    axis: Axis.vertical,
    child: widget.contentWidget
    )
 *
 *
 */
}
